package com.lcxedu.notebook.controller;

import com.lcxedu.notebook.entity.EventCategory;
import com.lcxedu.notebook.service.EventCategoryMapperService;
import com.lcxedu.notebook.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title: EventCategoryController
 * @Author: 李晨曦
 * @Date: 2025/6/11
 * 好好看 好好学 都能学会 都能学好
 * 我亦无他 唯手熟尔
 */
@RestController
public class EventCategoryController {
    @Resource
    private EventCategoryMapperService eventCategoryMapperService;

    // 查询所有类别
    @GetMapping("/category/list")

    public Result list() {
        List<EventCategory> eventCategories = eventCategoryMapperService.selectAll();
        return Result.success(eventCategories);
    }

    // 添加类别
    @PostMapping("/category/add")
    public Result add(@RequestBody EventCategory category) {
        int insert = eventCategoryMapperService.insert(category);
        if (insert > 0) {
            return Result.success();
        }
        return Result.error("400","添加失败");
    }

    // 修改类别
    @PutMapping("/category/update")
    public Result update(@RequestBody EventCategory category) {
        int i = eventCategoryMapperService.updateByPrimaryKeySelective(category);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("400","修改失败");
    }

    // 删除类别
    @DeleteMapping("/category/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int i = eventCategoryMapperService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("400","删除失败");
    }
}
